<?php
/**
 * Created by PhpStorm.
 * User: dapeng
 * Date: 2018/10/18
 * Time: 下午2:22
 */

namespace app\controllers\Project_1_0;

use app\models\Project_1_0\Cases as MCases;
use Core\Controller;

class Cases extends Controller
{
    private $pagenum = 5;//每页显示10条记录

    function __construct()
    {
        parent::__construct();
        if (!isset($_SESSION['user'])){
            $this->error("您尚未登录","index.php?action=user&func=login");
            exit();
        }

    }


    function index11(){
        //查询数据库(调用模型)
        $cases = new MCases();
        $res = $cases -> findAll();

        $tp = loadLibrary("Mytp");
        $tp->assign("res",$res);
        $tp->tp("cases/user-list");
//        $tp->tp("cases/index");
    }

    //案件的列表
//    index.php?action=Cases&func=index
    function index(){
        $page1 = isset($_GET['page']) ? $_GET['page'] : 1;
//        echo $page1;die();
        $page =$this->pagenum;
        $cases = new MCases();
        $cc = ($page1 - 1) * $page;
        $res = $cases ->lists($cc,$page);
        $count = $cases -> count_page();
        $count_page = ceil($count[0]['count(*)'] / $page);
        if($page1 > $count_page){
            $page1 = $count_page;
        }

        //查询数据库(调用模型)
        $tp = loadLibrary("Mytp");
        $tp->assign("res",$res);
        $tp->assign("count_page",$count_page);
        $tp->assign("current",1);
        $tp->assign("page",$page1);
        $tp->tp("cases/user-list");
//        $tp->tp("cases/index");
    }

    function search(){
        //获取页面传过来的查询条件
        $option = $_POST['option'];//案件号、承办人、状态
        $content = $_POST['content'];//查询的内容

        $cases = new MCases();
        $res = $cases->search($option,$content);

        $tp = loadLibrary("Mytp");
        $tp->assign("res",$res);
        $tp->assign("current",1);
        $tp->tp("cases/user-list");
    }

    function add(){
        $tp = loadLibrary("Mytp");
        $tp->assign("current",1);
        $tp->tp("cases/add");
    }

    function addcases(){
        $tp = loadLibrary("Mytp");
        $tp->assign("current",1);
        $tp->tp("cases/addcases");
    }



    function save(){

        if(isset ($_POST['dealer']))
        {
            if(isset ($_POST['name']))
            {
                if(isset ($_POST['except']))
                {   $dealer = $_POST['dealer'];
                    $name = $_POST['name'];
                    $except = $_POST['except'];
                    $status="已分配";
                    $cases = new \app\models\Project_1_0\Cases();
                    $res = $cases->add($dealer,$name,$except,$status);
                    if($res > 0){
                        //跳转页面
                        echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php?action=cases&func=index\">";
                        print('添加成功,正在拼命的跳转页面...<br>1秒后自动跳转~~~');
                    }else{
                        echo "添加失败";
                    }
                }else{
                    die("请输入计划完成日期");
                }
            }else{
                die("请输入案件号");
            }
        }else{
            die("请输入承办人");
        }






    }
    //跳转页面
    function editeq(){
        $id = isset($_REQUEST['c_id']) ? $_REQUEST['c_id'] : false;
        $cases = new MCases();
        $edit = $cases ->  edit($id);
        $tp = loadLibrary("Mytp");
        $tp->assign("edit",$edit);
        $tp->assign("current",1);
        $tp->tp("cases/editcases");

    }
    //更新
    function edit(){
        $id = isset($_REQUEST['c_id']) ? $_REQUEST['c_id'] : false;
        $Name = isset($_REQUEST['name']) ? $_REQUEST['name'] : false;
        $dealer = isset($_REQUEST['dealer']) ? $_REQUEST['dealer'] : false;
        $receive_date = isset($_REQUEST['receive_date']) ? $_REQUEST['receive_date'] : false;
        $finish_date = isset($_REQUEST['finish_date']) ? $_REQUEST['finish_date'] : false;
        $except_date = isset($_REQUEST['except_date']) ? $_REQUEST['except_date'] : false;
        $status = isset($_REQUEST['status']) ? $_REQUEST['status'] : false;
        $cases = new MCases();
        $res = $cases ->update($id,$Name,$dealer,$receive_date,$finish_date,$except_date,$status);
        if($res > 0){
            //跳转页面
            echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php?action=cases&func=index\">";
            print('编辑成功,正在拼命的跳转页面...<br>1秒后自动跳转~~~');

        }else{
            echo "编辑失败";
        }
    }

    //删除
    function delect(){
        $id = isset($_REQUEST['c_id']) ? $_REQUEST['c_id'] : false;
        $cases = new MCases();
        $res = $cases ->del($id);
        if ($res > 0){
            //跳转页面
            echo "<meta http-equiv=\"refresh\" content=\"1; url=index.php?action=cases&func=index\">";
            print('删除成功,正在拼命的跳转页面...<br>1秒后自动跳转~~~');
        }else{
            echo "删除失败";
        }
    }





}